delete uploaded media file wp using code

109

delete uploaded media file wp using code -

function delete_unattached_attachments(){
	$attachments = get_posts( array(
	'post_type' => 'attachment',						
	'numberposts' => -1,	 	  								
	'fields' => 'ids', 
	'post_parent' => 0,
		));									
		if ($attachments) {		 		
			foreach ($attachments as $attachmentID){
				$attachment_path = get_attached_file( $attachmentID); 
				//Delete attachment from database only, not file
				$delete_attachment = wp_delete_attachment($attachmentID, true);
				//Delete attachment file from disk
				$delete_file = unlink($attachment_path);					
			}					
		}				
}

Comments

Submit
0 Comments